package com.wing.member.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.member.model.entity.MemberRank;
import com.wing.member.service.MemberRankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author
 */
@Api(value = "Admin-用户等级管理", tags = "#/member/memberRank")
@RestController("memberRankController")
@RequestMapping("member/admin/memberRank")
public class MemberRankController {

    @Autowired
    private MemberRankService memberRankService;

    @ApiOperation(value = "查询所有用户等级", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<MemberRank> list(
        Integer page,
        Integer limit,
        String sort,
        String order,
        String name
    ){

        Page<MemberRank> pg = new Page<>(page, limit);
        QueryWrapper<MemberRank> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByAsc("sort_number");

        // 排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)){
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            }else{
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        // 搜索
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }

        IPage<MemberRank> list = memberRankService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加用户等级", notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("/add")
    public JsonResult add(MemberRank memberRank) {
        if (memberRankService.save(memberRank)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改用户等级", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "form")
    })
    @PutMapping("/update")
    public JsonResult update(MemberRank memberRank) {
        if (memberRankService.updateById(memberRank)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    //@ApiOperation(value = "删除用户等级")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "memberRankId", value = "用户等级id", required = true, dataType = "Integer", paramType = "path"),
    //        @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    //})
    //@DeleteMapping("/{id}")
    //public JsonResult delete(@PathVariable("id") Long memberRankId) {
    //    if (memberRankService.removeById(memberRankId)) {
    //        return JsonResult.success("删除成功");
    //    }
    //    return JsonResult.fail("删除失败");
    //}
}
